<?php
class Zend_View_Helper_LanguageSwitcher
{
	public $view;
	public $t;

	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->t = Zend_Registry::get('Zend_Translate');
	}

	public function languageSwitcher()
	{
		$languages = new Languages();
		$allLanguages = $languages->fetchAll();
		$html = "";

		$this->view->placeholder('jquery')->append("
			$('.languageSwitcher').click(function() {
				languageId = $(this).attr('id').substring(8);
				$.get('{$this->view->baseUrl()}/language/change/id/' + languageId, function() {
					window.location.reload();
				});

				return false;
			});
		");

		foreach($allLanguages as $language) {
			$html .= "
			<a id=\"language{$language->id}\" class=\"languageSwitcher\" href=\"#\" title=\"{$language->name}\">
				<img src=\"{$this->view->baseUrl()}/images/system/flag_{$language->abbreviation}.png\" alt=\"{$language->name}\" />
			</a>";
		}

		$html = "
		<div>
		{$this->t->_('change your language')}<br />
		{$html}
		</div>";

		return $html;
	}
}